<?php

$num = strss(4);

session_start();
$_SESSION['think']['code'] = strtoupper($num);
$out = iconv('gbk', 'utf-8', $num);
$hight = strlen($out) * 10;

$image = imagecreate($hight, 20);//创建一个大小为 100*20的图片，大小可更改
$bg = imagecolorallocate($image, 255, 255, 255);//定义白色背景和蓝色文字
$textcolor = imagecolorallocate($image, 55, 55, 55);
for ($i = 0; $i <= 128; $i++) {
    $point_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($image, rand(2, 128), rand(2, 38), $point_color);
}
imagestring($image, 5, 0, 3, $out, $textcolor);
header("Content-type: image/png");//输出PNG图片
imagepng($image);


/**
 * 默认生成6位随机字符串
 * @param int $length
 * @return string
 */
function strss($length = 6) {
    if (!is_int($length) || $length <= 0) return false;
    $str = '1234567890abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ';
    $substr = '';
    for ($i = 0; $i< $length; $i++) {
        $substr .= $str[mt_rand(0, strlen($str) - 1)];
    }
    return $substr;
}